package com.cxs.rdbdemo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;
import java.util.Set;

public class DataAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    private RdbStore rdbStore;
    RdbOpenCallback callback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            // 创建数据库时被回调，可以在该方法中初始化表结构，并添加一些应用使用到的初始化数据
            rdbStore.executeSql("CREATE TABLE IF NOT EXISTS user" +
                    "(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER, salary REAL)");

            rdbStore.executeSql("CREATE TABLE IF NOT EXISTS book" +
                    "(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, price REAL)");
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "DataAbility onStart");
        
        DatabaseHelper helper = new DatabaseHelper(this);
        StoreConfig config = StoreConfig.newDefaultConfig("rdbstoretest.db");
        rdbStore = helper.getRdbStore(config, 1, callback);

    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        // 把DataAbilityPredicates转换为RdbPredicates
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, uri.getLastPath());
        ResultSet query = rdbStore.query(rdbPredicates, columns);
        return query;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        if (rdbStore == null) {
            return -1;
        }
        long index = rdbStore.insert(uri.getLastPath(), value);
        return (int)index;
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, uri.getLastPath());
        int delete = rdbStore.delete(rdbPredicates);
        return delete;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, uri.getLastPath());
        int update = rdbStore.update(value, rdbPredicates);
        return update;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}